package com.hym.mediademo;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private List<MediaPlayer> medias=new ArrayList<MediaPlayer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    public void initView(){
        findViewById(R.id.btn_play_audio).setOnClickListener(this);
        findViewById(R.id.btn_play_audio_2).setOnClickListener(this);
        findViewById(R.id.btn_play_audio_3).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_play_audio:
                //通过create方法创建的mediaPlayer 进入到prepared的状态
                MediaPlayer mediaPlayer=MediaPlayer.create(this,R.raw.girl_audio);
                medias.add(mediaPlayer);
                //直接start
                mediaPlayer.start();
                break;
            case R.id.btn_play_audio_2:
                //audioPath=http://www.boomq.com//resources/audio/template/01.amr
                String url="http://www.boomq.com/resources/audio/template/02.amr";
                //通过new方法创建的mediaPlayer 进入到prepared的状态
                MediaPlayer mediaPlayer2=new MediaPlayer();
                mediaPlayer2.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mediaPlayer2.setDataSource(url);
                    //先prepare 同步的prepare 可能花费较多时间，需要准备缓冲区
                    mediaPlayer2.prepare();
                    //note：网络权限没有添加的时候，会报错java.io.IOException: setDataSource failed.: status=0x80000000
                    mediaPlayer2.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                break;
            case R.id.btn_play_audio_3:
                //audioPath=http://www.boomq.com//resources/audio/template/01.amr
                String url3="http://www.boomq.com/resources/audio/template/02.amr";
                //通过new方法创建的mediaPlayer 进入到prepared的状态
                final MediaPlayer mediaPlayer3=new MediaPlayer();
                mediaPlayer3.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mediaPlayer3.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer3.start();
                    }
                });
                //It's important to remember that when an error occurs, the MediaPlayer moves to the Error state
                //(see the documentation for the MediaPlayer class for the full state diagram) and you must reset
                // it before you can use it again.
                mediaPlayer3.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        mediaPlayer3.reset();
                        return false;
                    }
                });
                try {
                    mediaPlayer3.setDataSource(url3);//initialized state
                    //先prepare 同步的prepare 可能花费较多时间，需要准备缓冲区
                    mediaPlayer3.prepareAsync();//preparing to prepared state
                    //note：网络权限没有添加的时候，会报错java.io.IOException: setDataSource failed.: status=0x80000000

                } catch (IOException e) {
                    Log.e("exception:",e.getMessage());
                }
        }
//        String url="http://www.boomq.com/apollo/video/2016/5/27/310d8194db424342a0a2fed472929d4a.mp4";
    }


    @Override
    protected void onDestroy() {
        if(medias.size()>0){
            for (MediaPlayer mediaPlayer:medias) {
                mediaPlayer.release();
                //思考这种方式，是否能够被回收
                mediaPlayer=null;
            }
        }
        super.onDestroy();
    }
}
